<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '用户添加' : '用户编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="所属角色" label-width="120px">
          <el-select
            v-model="user.roleid"
            placeholder="请选择"
          >
            <el-option
              v-for="item in arr"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { menulist, roleadd, roleedit, roleinfo, rolelist, useradd, useredit, userinfo, userlist } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      user: {
        roleid:"",
        username: "",
        password:"",
        status: 1,
      },
      arr:[]
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),

    cancel() {
      this.info.isshow = false;
    },
    add() {
      useradd(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.info.isshow = false;
          this.$emit("init");
          this.empty();
        }
      });
    },
    update() {
      useredit(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //判断修改的user.uid是不是当前登录用户的uid，如果是就退出，不是就什么不做
          if(this.user.uid==this.userInfo.uid){
            this.changeUserInfo({});
            this.$router.replace("/login");
            return
          }
          this.info.isshow = false;
          this.$emit("init");
          this.empty();
        }
      });
    },
    empty() {
      this.user = {
        roleid:"",
        username: "",
        password:"",
        status: 1,
      };
    },
    getOne(uid) {
      userinfo({ uid }).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          this.user.uid = uid;
          this.user.password="";
        }
      });
    },

    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
    rolelist().then(res=>{
      if(res.data.code==200){
        this.arr=res.data.list;
      }
    })
  },
};
</script>

<style>
</style>